#include "EmbryoIndividualDevelopment.h"

#include "EmbryoIndividual.h"

#include "../../../physics/cpm/Cell.h"
#include "../../../physics/cpm/Physics.h"
//#include "../../../physics/cpm/shapes/ShapeIdentifier.h"
//#include "../../../physics/cpm/shapes/ShapeIdentifierGenerator3X3.h"

namespace GA {

/*
	EmbryoIndividualDevelopment
*/

/*	public	*/

EmbryoIndividualDevelopment::EmbryoIndividualDevelopment(
	int worldWidth,
	int worldHeight,
	EmbryoIndividual const& individual,
	int developmentalIterations) :
	_world(worldWidth, worldHeight),
	_individual(individual),
	_developmentalIterations(developmentalIterations),
	_currentIterations(0) {

	/* physics */
	_physics = new Physics(_world);

//	/* shape identifier */
//	ShapeIdentifierGenerator3X3& generator = ShapeIdentifierGenerator3X3::getInstance();
//	_shapeIdentifier                       = generator.getNewShapeIdentifier();

	/* cell */
//	int cellSide = 1;
//	int cellSide = 3;
	int cellSide = 6;
	int xCellOrigin = worldWidth  / 2 - cellSide / 2;
	int yCellOrigin = worldHeight / 2 - cellSide / 2;
	Cell* cell = Cell::newCellSquare(_world, xCellOrigin, yCellOrigin, cellSide);
	cell->setController(individual.getGenome().getNewCellController());

//	/* worldly cell */
//	_world.addCell(cell);
//	int cellId = cell->getId();
//	int xCellOrigin = worldWidth  / 2 - cellSide / 2;
//	int yCellOrigin = worldHeight / 2 - cellSide / 2;
//	for(int i = 0; i != cellSide; ++i) {
//		for(int j = 0; j != cellSide; ++j) {
//			_world.setTile(xCellOrigin + i, yCellOrigin + j, cellId);
//		}
//	}

}

EmbryoIndividualDevelopment::~EmbryoIndividualDevelopment() {
	delete _physics;
//	delete _shapeIdentifier;
}

void EmbryoIndividualDevelopment::runOnce() {
	if (_currentIterations != _developmentalIterations) {
		_physics->runOnce();
		++_currentIterations;
	}
}

void EmbryoIndividualDevelopment::runAll() {
	while(_currentIterations != _developmentalIterations) {
		_physics->runOnce();
		++_currentIterations;
	}
}

bool EmbryoIndividualDevelopment::isFinished() const {
	return _currentIterations == _developmentalIterations;
}

World const& EmbryoIndividualDevelopment::getWorld() const {
	return _world;
}

} // GA

